<template>
  <view class="measurement-record-container">
    <status-bar bgColor="#0088ff"></status-bar>
    
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="back-btn" @click="goBack">
        <image src="/static/images/icon/backward_flat.png" class="back-icon-img"></image>
      </view>
      <view class="nav-title">测量记录</view>
      <view class="add-btn" @click="addMeasurementData" v-if="orderStatus != '5'">添加</view>
    </view>
    
    <!-- 测量数据列表 -->
    <view class="data-list">
      <view class="data-item" v-for="(item, index) in measurementData" :key="index" @click="viewItemDetail(item)">
        <!-- 房间图标和信息 -->
        <view class="room-info">
          <view class="room-icon-container">
            <image :src="item.roomIcon" class="room-icon"></image>
            <text class="room-label">{{getWindowName(item.windowType)}}</text>
          </view>
          
          <view class="room-details">
            <view class="room-name">{{item.roomNo}}</view>
            <view class="product-info">
              <text class="product-name">{{getProductTypeName(item.productType)}} | {{item.plies == '1' ? '单层':'双层'}} | {{getinstallWayName(item.installWay)}} | {{item.openWay == '2'?'单开':'双开' }}</text>
            </view>
            <view class="size-info">
              <text class="size-text">宽度：{{item.windowWidth}} 米  | {{ getWidthTypeName(item.installScenario, item.widthSizeType) }}</text>
            </view>
            <view class="size-info">
              <text class="size-text">高度：{{item.windowHeight}} 米 | {{ getHeightTypeName(item.installScenario, item.heightSizeType) }} </text>
            </view>
          </view>
        </view>
        
        <!-- 费用信息 -->
        <!-- <view class="cost-info">
          <text class="cost-label">费用：</text>
          <text class="cost-value">¥ {{item.measureFee}}</text>
          <image src="/static/images/icon/arrow_right_gray.png" class="arrow-icon"></image>
        </view> -->
      </view>
    </view>
    
    <!-- 底部操作按钮 -->
    <view class="bottom-actions" v-if="false">
      <button class="action-btn add-btn" @click="addMeasurementData">
        <text>添加测量数据</text>
      </button>
    </view>
  </view>
</template>

<script>
import { getOrderDataList } from '@/api/app/master/order'
import { confgiList } from '@/api/app/master/common'
import { allSystemParamConfig } from '@/api/app/master/common'

export default {
  data() {
    return {
	  orderNo: undefined,
      orderId: null,
<<<<<<< HEAD
      orderStatus: '',
=======
      orderStatus: undefined,
>>>>>>> f085353b7dcb889819febcba0aedb7d754613b55
      measurementData: [],
      configData: [],
      productType: [],
      // 系统参数配置列表
      systemParamConfigList: [],
      // getAllSystemParamConfig方法中使用的变量
      sysParamConfigs: [],
      projectNames: [],
      specialWalls: [],
      curtainsTypes: [],
      measureProjects: [],
      measureTypes: [],
      windowTypes: [],
      installSurfaceTypes: [],
      installScenarios: [],
      widthSizeTypes: [],
      heightSizeTypes: [],
      additionalChildFees: [],
      additionalFees: [],

      installWays:[
        {
          value: "1",
          label: "顶装"
        },
        {
          value: "2",
          label: "侧装"
        },
        {
          value: "4",
          label: "两头安装"
        },
        {
          value: "5",
          label: "免打孔"
        },
        {
          value: "3",
          label: "已安装"
        }

      ],
      
    }
  },
  
  onLoad(options) {

    this.getAllSystemParamConfig();

    if (options.orderId) {
      this.orderId = options.orderId;
      this.orderStatus = options.orderStatus;
      // 获取配置数据
      this.getConfigData();
      // 获取产品类型  
      this.getProductTypeData();
      // 获取测量数据
      this.getMeasurementData();
    }
	if(options.orderNo){
		this.orderNo=options.orderNo;
	}
  },
  
  
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },

    getAllSystemParamConfig() {
      uni.showLoading({
        title: '加载配置中...'
      });
      
      allSystemParamConfig().then(response => {
        this.sysParamConfigs = response.data;
        // 处理所有类型的数据
        this.projectNames = [];
        this.specialWalls = [];
        this.curtainsTypes = [];
        this.measureProjects = [];
        this.measureTypes = [];
        this.additionalChildFees = [];
        this.additionalFees = [];
        
        for (let i = 0; i < this.sysParamConfigs.length; i++) {
          let tempConfig = this.sysParamConfigs[i];
          // 产品名称
          if (tempConfig.parentSign === 'productType') {
            this.projectNames.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }
          // 特殊墙体
          if (tempConfig.parentSign === 'specialWall') {
            this.specialWalls.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }
          // 窗帘类型
          if (tempConfig.parentSign === 'curtainsType') {
            this.curtainsTypes.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }
          // 测量项目
          if (tempConfig.parentSign === 'measureProject') {
            this.measureProjects.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }
        //   // 测量类型
        //   if (tempConfig.parentSign === 'measureType') {
        //     this.measureTypes.push({
        //       value: tempConfig.paramValue,
        //       label: tempConfig.paramName
        //     });
        //   }
		//窗户类型
          if (tempConfig.parentSign === 'windowType') {
            this.windowTypes.push({
              value: tempConfig.paramSign,
              label: tempConfig.paramName
            });
          }

		  //安装面材质
          if (tempConfig.parentSign === 'installSurfaceType') {
            this.installSurfaceTypes.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }

		  //安装场景
          if (tempConfig.parentSign === 'installScenario') {
            this.installScenarios.push({
              value: tempConfig.paramValue,
              label: tempConfig.paramName
            });
          }

		      //宽度尺寸类型
          if (tempConfig.parentSign === 'widthSizeType') {
            for (let j = 0; j < this.sysParamConfigs.length; j++) {
              let tempConfigChild = this.sysParamConfigs[j];
              if (tempConfigChild.parentSign !== tempConfig.paramSign) {
                continue;
              }
              this.widthSizeTypes.push({
                parentSign: tempConfig.paramSign,
                parentName: tempConfig.paramName,
                value: tempConfigChild.paramValue,
                label: tempConfigChild.paramName
              });
            }
          }
          //高度尺寸类型
          if (tempConfig.parentSign === 'heightSizeType') {
            for (let j = 0; j < this.sysParamConfigs.length; j++) {
              let tempConfigChild = this.sysParamConfigs[j];
              if (tempConfigChild.parentSign !== tempConfig.paramSign) {
                continue;
              }
              this.heightSizeTypes.push({
                parentSign: tempConfig.paramSign,
                parentName: tempConfig.paramName,
                value: tempConfigChild.paramValue,
                label: tempConfigChild.paramName
              });
            }
          }


        }
        
        uni.hideLoading();
      }).catch(err => {
        console.error('获取系统配置失败:', err);
        uni.hideLoading();
        uni.showToast({
          title: '获取配置失败',
          icon: 'none'
        });
      });
    },

    // 获取配置数据列表
    getConfigData() {
      confgiList({
        parentSign:'windowType'
      })
        .then(res => {
          if (res && res.code === 200) {
            this.configData = Array.isArray(res.data) ? res.data : [];
          } else {
            this.configData = [];
          }
        })
        .catch(() => {
          this.configData = [];
        });
    },
    
    getProductTypeData() {
      confgiList({
        parentSign:'productType'
      })
        .then(res => {
          if (res && res.code === 200) {
            this.productType = Array.isArray(res.data) ? res.data : [];
          } else {
            this.productType = [];
          }
        })
        .catch(() => {
          this.productType = [];
        });
    },

    getProductTypeName(val){
        for(let i=0;i<this.productType.length;i++){
            if(this.productType[i].paramValue==val){ 
                return this.productType[i].paramName;
            }
        }
        return '';
    },
    
    // 获取测量数据
    getMeasurementData() {
      if (!this.orderId) {
        uni.showToast({
          title: '订单ID不存在',
          icon: 'none'
        });
        return;
      }
      
      uni.showLoading({
        title: '加载中...'
      });
      
      getOrderDataList(this.orderId)
        .then(res => {
          if (res.code === 200) {
            // 处理接口返回的数据
            this.processMeasurementData(res.data.orderDataList);
            this.orderStatus = res.data.order.orderStatus
            console.log(this.orderStatus)
          } else {
            uni.showToast({
              title: res.msg || '获取数据失败',
              icon: 'none'
            });
          }
        })
        .catch(err => {
          console.error('获取测量数据失败:', err);
          uni.showToast({
            title: '获取数据失败',
            icon: 'none'
          });
        })
        .finally(() => {
          uni.hideLoading();
        });
    },
    
    // 处理测量数据
    processMeasurementData(data) {
      if (!data || !Array.isArray(data)) {
        this.measurementData = [];
        return;
      }
      console.log(data)
      // 将接口数据转换为页面需要的格式
      this.measurementData = data.map((item, index) => {
        return {
          id: item.id,
		  dataPicture: item.dataPicture || '',
          roomNo: item.roomNo,
          windowType: item.windowType,
		  installSurfaceType:  item.installSurfaceType,
		  installScenario: item.installScenario,
		  boxWidth: item.boxWidth,
		  boxHeight: item.boxHeight,
		  otherDistance: item.otherDistance,
          productType: item.productType,
		  leftSize: item.leftSize,
		  rightSize: item.rightSize,
		  frontSize: item.frontSize,
		  propelWay: item.propelWay,
		  powerDirection: item.powerDirection,
		  powerDistance: item.powerDistance,
		  plies: item.plies,
		  openWay: item.openWay,
		  openDirection: item.openDirection,
		  installWay: item.installWay,
		  widthSizeType: item.widthSizeType,
		  windowWidth: item.windowWidth,
		  heightSizeType: item.heightSizeType,
		  windowHeight: item.windowHeight,
		  curtainGround: item.curtainGround,
		  curtainGroundDistance: item.curtainGroundDistance,
		  isCurtains: item.isCurtains,
		  curtainsTrackSize: item.curtainsTrackSize,
		  isOpenInside: item.isOpenInside,
		  upperEdgeDistance: item.upperEdgeDistance,
		  specialDemand: item.specialDemand,
		  
          measureFee: item.measureFee,         
          roomLabel: this.getRoomLabel(item.roomName),
          roomIcon: this.getRoomIcon(item.roomName),
          productName: item.productName || '未知产品',
          width: item.width ? `${item.width}CM` : '0CM',
          height: item.height ? `${item.height}CM` : '0CM',
          cost: item.cost
        };
      });
    },
    
    // 获取房间标签
    getRoomLabel(roomName) {
      const labelMap = {
        '客厅': '客厅窗',
        '主卧': '飘窗',
        '次卧': '一字窗',
        '厨房': '厨房窗',
        '卫生间': '卫生间窗',
        '书房': '书房窗',
        '阳台': '阳台窗'
      };
      return labelMap[roomName] || '窗户';
    },
    
    // 获取房间图标
    getRoomIcon(roomName) {
      const iconMap = {
        '客厅': '/static/images/icon/living_room.png',
        '主卧': '/static/images/icon/bedroom.png',
        '次卧': '/static/images/icon/bedroom2.png',
        '厨房': '/static/images/icon/kitchen.png',
        '卫生间': '/static/images/icon/bathroom.png',
        '书房': '/static/images/icon/study.png',
        '阳台': '/static/images/icon/balcony.png'
      };
      return iconMap[roomName] || '/static/images/icon/room_default.png';
    },
    
    // 查看详情
    viewItemDetail(item) {
      uni.navigateTo({
        url: `/pages/order/measurement-edit?data=${encodeURIComponent(JSON.stringify(item))}&configs=${encodeURIComponent(JSON.stringify(this.sysParamConfigs))}&orderNo=${this.orderNo}&orderStatus=${this.orderStatus}`,
		events: {
			backFromAction: (state) => {
				console.log('backFromAction', state)
				if (state) {
					this.getMeasurementData();
				}
			}
		}
		
      });
    },
    
    // 添加测量数据
    addMeasurementData() {
		let url;
		const length = this.measurementData.length;
		if(length > 0){
			url = `/pages/order/measurement-upload?data=${encodeURIComponent(JSON.stringify(this.measurementData[length - 1]))}&configs=${encodeURIComponent(JSON.stringify(this.sysParamConfigs))}&orderNo=${this.orderNo}`;
		} else {
			url = `/pages/order/measurement-upload?configs=${encodeURIComponent(JSON.stringify(this.sysParamConfigs))}&orderNo=${this.orderNo}`;
		}
      uni.navigateTo({
        url: url,
		events: {
			backFromAction: (state) => {
				if (state) {
					this.getMeasurementData();
				}
			}
		}
      });
    },


    getWindowName( val ){
      for(let i=0;i<this.windowTypes.length;i++){
            if(this.windowTypes[i].value==val){ 
                return this.windowTypes[i].label;
            }
        }

    },
    getInstallScenario(value) {
      const item = this.installScenarios.find(item => item.value === value);
      return item ? item.label : '未知';
    },
    getinstallWayName( val ){
      for(let i=0;i<this.installWays.length;i++){
            if(this.installWays[i].value==val){ 
                return this.installWays[i].label;
            }
        }

    },
    getWidthTypeName( installScene ,  val ){
      const types = [];
	   //根据安装场景加载高度尺寸类型
      for (let i = 0; i < this.widthSizeTypes.length; i++) {
        if (this.getInstallScenario(installScene) === this.widthSizeTypes[i].parentName) {
          types.push(this.widthSizeTypes[i]);
        }
      }
      for(let i=0;i<types.length;i++){
            if(types[i].value==val){ 
                return types[i].label;
            }
        }
    },
    getHeightTypeName( installScene ,  val ){
      const types = [];
	   //根据安装场景加载高度尺寸类型
      for (let i = 0; i < this.heightSizeTypes.length; i++) {
        if (this.getInstallScenario(installScene) === this.heightSizeTypes[i].parentName) {
          types.push(this.heightSizeTypes[i]);
        }
      }
      for(let i=0;i<types.length;i++){
            if(types[i].value==val){ 
                return types[i].label;
            }
        }
    },
    

  },

  



}
</script>

<style lang="scss" scoped>
.measurement-record-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
}

/* 顶部导航栏 */
.nav-bar {
  display: flex;
  align-items: center;
  height: 90rpx;
  background-color: #0088ff;
  color: #fff;
  padding: 0 30rpx;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 30rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon-img {
  width: 40rpx;
  height: 40rpx;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
}

.add-btn {
  font-size: 32rpx;
  text-align: right;
  color: #fff;
}

/* 数据列表 */
.data-list {
  padding: 20rpx;
}

.data-item {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* 房间信息 */
.room-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.room-icon-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 30rpx;
  min-width: 120rpx;
}

.room-icon {
  width: 80rpx;
  height: 80rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 8rpx;
  background-color: #f8f8f8;
  margin-bottom: 10rpx;
}

.room-label {
  font-size: 24rpx;
  color: #999;
  text-align: center;
}

.room-details {
  flex: 1;
}

.room-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 8rpx;
}

.product-info {
  margin-bottom: 6rpx;
}

.product-name {
  font-size: 28rpx;
  color: #666;
}

.size-info {
  margin-bottom: 4rpx;
}

.size-text {
  font-size: 26rpx;
  color: #888;
}

/* 费用信息 */
.cost-info {
  display: flex;
  align-items: center;
  min-width: 160rpx;
  justify-content: flex-end;
}

.cost-label {
  font-size: 28rpx;
  color: #666;
  margin-right: 8rpx;
}

.cost-value {
  font-size: 32rpx;
  font-weight: bold;
  color: #ff3b30;
  margin-right: 15rpx;
}

.arrow-icon {
  width: 24rpx;
  height: 24rpx;
  opacity: 0.4;
}

/* 底部操作按钮 */
.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-top: 1rpx solid #f0f0f0;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.action-btn {
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 30rpx;
  border: none;
}

.add-btn {
  background-color: #0088ff;
  color: #fff;
}

.add-btn:active {
  background-color: #0066cc;
}
</style>